﻿using Basic.WxModel.DB;
using Basic.WxModel.WxFilter;
using WeDonekRpc.Helper;
using WeDonekRpc.Helper.IdGenerator;
using WeDonekRpc.SqlSugar;

namespace Basic.WxRepository.lmpl
{
    internal class WxFilterRepository : WxBasicRepository<DBWxMsgSubscribe, long>, IWxFilterRepository
    {
        public WxFilterRepository (ISqlClientFactory factory) : base(factory)
        {
        }

        public long FindId (long rpcMerId, string systemType)
        {
            return base.Get(a => a.RpcMerId == rpcMerId && a.SystemType == systemType, c => c.Id);
        }

        public void Add (WxFilterAdd add)
        {
            DBWxMsgSubscribe data = new DBWxMsgSubscribe
            {
                AddTime = DateTime.Now,
                SystemType = add.SystemType,
                Id = IdentityHelper.CreateId(),
                RpcMerId = add.RpcMerId
            };
            ISqlQueue<DBWxMsgSubscribe> queue = base.BeginQueue();
            queue.Insert(data);
            queue.Insert<DBWxMsgSubscribeFilter>(add.Adds.ConvertAll(c =>
            {
                return new DBWxMsgSubscribeFilter
                {
                    AddTime = data.AddTime,
                    Id = IdentityHelper.CreateId(),
                    Direct = c.Direct,
                    SubscribeId = data.Id,
                    AppId = c.AppId,
                    FilterType = c.FilterType,
                    ForwardType = c.ForwardType,
                    LastUpdateTime = data.AddTime.ToLong(),
                    MsgType = c.MsgType,
                    Reply = c.Reply,
                    WxAppId = c.WxAppId
                };
            }));
            _ = queue.Submit();
        }
    }
}
